package com.demo.apparel.controller.uniapp;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

@RestController
@RequestMapping("/wx/weather")
public class WeatherController {

    private final WebClient webClient;

    @Value("${tencent.map.key}")
    private String tencentMapKey;

    public WeatherController(WebClient.Builder builder) {
        this.webClient = builder.baseUrl("https://apis.map.qq.com").build();
    }

    @GetMapping
    public Mono<String> getWeather(@RequestParam double latitude,
                                   @RequestParam double longitude) {
        String location = latitude + "," + longitude;
        System.out.println("latitude=" + latitude + ", longitude=" + longitude);
        return webClient.get()
                .uri(uriBuilder -> uriBuilder
                        .path("/ws/weather/v1/")
                        .queryParam("location", location)
                        .queryParam("key", tencentMapKey)
                        .build())
                .retrieve()
                .bodyToMono(String.class);
    }
}

